using System.Collections.Generic;
using System.Collections.Specialized;

namespace HooIzDat.Web.Models
{
    public partial class TraitVariation
    {
        public virtual int TraitVariationID
        {
            get;
            set;
        }
    
        public virtual int TraitID
        {
            get { return _traitID; }
            set
            {
                if (_traitID != value)
                {
                    if (Trait != null && Trait.TraitID != value)
                    {
                        Trait = null;
                    }
                    _traitID = value;
                }
            }
        }
        private int _traitID;
    
        public virtual string Name
        {
            get;
            set;
        }

        public virtual ICollection<CharacterTrait> CharacterTraits
        {
            get
            {
                if (_characterTraits == null)
                {
                    var newCollection = new FixupCollection<CharacterTrait>();
                    newCollection.CollectionChanged += FixupCharacterTraits;
                    _characterTraits = newCollection;
                }
                return _characterTraits;
            }
            set
            {
                if (!ReferenceEquals(_characterTraits, value))
                {
                    var previousValue = _characterTraits as FixupCollection<CharacterTrait>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupCharacterTraits;
                    }
                    _characterTraits = value;
                    var newValue = value as FixupCollection<CharacterTrait>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupCharacterTraits;
                    }
                }
            }
        }
        private ICollection<CharacterTrait> _characterTraits;
    
        public virtual Trait Trait
        {
            get { return _trait; }
            set
            {
                if (!ReferenceEquals(_trait, value))
                {
                    var previousValue = _trait;
                    _trait = value;
                    FixupTrait(previousValue);
                }
            }
        }
        private Trait _trait;

        private void FixupTrait(Trait previousValue)
        {
            if (previousValue != null && previousValue.TraitVariations.Contains(this))
            {
                previousValue.TraitVariations.Remove(this);
            }
    
            if (Trait != null)
            {
                if (!Trait.TraitVariations.Contains(this))
                {
                    Trait.TraitVariations.Add(this);
                }
                if (TraitID != Trait.TraitID)
                {
                    TraitID = Trait.TraitID;
                }
            }
        }
    
        private void FixupCharacterTraits(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (CharacterTrait item in e.NewItems)
                {
                    item.TraitVariation = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (CharacterTrait item in e.OldItems)
                {
                    if (ReferenceEquals(item.TraitVariation, this))
                    {
                        item.TraitVariation = null;
                    }
                }
            }
        }
    }
}
